﻿#region Using Directives

using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.TeamArchitect.PowerTools.Features;

#endregion

namespace FeatureBuilder.ContribLib.ValueProviders
{
    /// <summary>
    /// Provides the first defined active document selected in the selected Project
    /// </summary>
    /// 
    [DisplayName("CurrentItemProvider")]
    [Description("Provides the first defined active document selected in the selected Project")]
    [Category("IDE")]
    public class CurrentItemProvider : ValueProvider
    {
        /// <summary>
        /// Gets the ServiceProvider that we will use to get a reference to DTE.
        /// </summary>
        [Import(typeof(SVsServiceProvider))]
        public IServiceProvider ServiceProvider { get; private set; }

        #region Overrides

        /// <summary>
        /// Return the selected Document.
        /// </summary>
        public override object Evaluate()
        {
            object selectedItem = null;
            DTE projectDTE = this.ServiceProvider.GetService(typeof(DTE)) as DTE;

            if (projectDTE != null)
            {
                selectedItem = projectDTE.SelectedItems.Item(1).ProjectItem as ProjectItem;
            }
            return selectedItem;
        }

        #endregion
    }
}
